/**
 * Copyright (C) 2012 Oleg Anastassov <o.anastasov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.ent.desktop.ui.dialog.environment;

import kz.kstu.ent.desktop.images.GUIFactory;
import kz.kstu.ent.desktop.images.GUIIcon;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.Properties;

/**
 * Date: 25.07.12
 * Time: 22:20
 *
 * @author Oleg Anastassov
 */
public class EnvironmentDialog extends JDialog {

    private static final String TITLE = "<ЗАГОЛОВОК> :: Настройки окружения";

    private static final int WIDTH = 600;
    private static final int HEIGHT = 500;

    private Object[] headers = new String[]{
            "Переменная", "Значение"
    };

    private JTable table;
    private JButton buttonClose;
    private JButton buttonSave;
    private JPanel rootPanel;

    private Properties props = System.getProperties();

    public EnvironmentDialog() {
        props = System.getProperties();
    }

    public void initStaticComponents() {
        setContentPane(rootPanel);
        setTitle(TITLE);
        setModal(true);
        setSize(WIDTH, HEIGHT);
        setResizable(false);

        initIcons();
        initListeners();
    }

    private void initIcons() {
        setIconImage(
                GUIFactory.getImage(GUIIcon.ENVIRONMENT)
        );
        buttonSave.setIcon(
                GUIFactory.getIcon(GUIIcon.SAVE)
        );
        buttonClose.setIcon(
                GUIFactory.getIcon(GUIIcon.CANCEL)
        );
    }

    private void initListeners() {
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onClose();
            }
        });

        buttonClose.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onClose();
            }
        });

        buttonSave.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onSave();
            }
        });
    }

    public void initData() {
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.setColumnIdentifiers(headers);

        Enumeration em = props.keys();
        while (em.hasMoreElements()) {
            String key = (String) em.nextElement();
            String val = (String) props.get(key);
            dtm.addRow(new String[]{key, val});
        }
        table.setModel(dtm);
        table.setEnabled(false);
    }

    private void onClose() {
        dispose();
    }

    private void onSave() {
        try {
            Calendar cal = Calendar.getInstance();
            String fileName = "ent-environment";
            String fileExt = ".properties";
            FileOutputStream out = new FileOutputStream(
                    fileName + "-" + cal.getTime().getTime() + fileExt
            );
            props.store(out, "ENT Application");
            out.close();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(
                    rootPane,
                    "Ошибка сохранения в файл.",
                    "Ошибка",
                    JOptionPane.ERROR_MESSAGE
            );
            return;
        }
        JOptionPane.showMessageDialog(
                rootPane,
                "Файл успешно сохранен",
                "Сохранение",
                JOptionPane.INFORMATION_MESSAGE
        );
    }

}
